<?php
include ('../pages/check_login.include.php');

include ('../classes/autoloading_pages.include.php');

if (!$logged) {
    header('Location: /eventijesolo/login/');
    exit();
}

$hm = new HTMLElementsManager();
$navigation = $hm->get_HTML_element_by_name('navigation');
$footer = $hm->get_HTML_element_by_name('footer');

define (ACTION, 'action');
define (ACTION_EDIT, 'edit');
define (ACTION_NEW, 'new');
define (ACTION_SAVE, 'save');
define (ARTICLE_ID, 'id');
define (ARTICLE_SEO, 'seo');

$through_controller = TRUE;

if (isset($_GET[ACTION])) {
    $am = new ArticlesManager();
    if($_GET[ACTION] == ACTION_EDIT){
        if(isset($_GET[ARTICLE_ID])) {
            $article = $am->get_article_by_id($_GET[ARTICLE_ID], null, true);
        } else {
            echo 'error';
        }
    } else if ($_GET[ACTION] == ACTION_SAVE) {
        if (isset($_GET[ARTICLE_ID])) {
            $article = $am->get_article_by_id($_GET[ARTICLE_ID], null, true);
            $article->title = (isset($_POST['title'])) ? $_POST['title'] : 'No Title';
            $article->abstract = (isset($_POST['abstract'])) ? $_POST['abstract'] : 'No Abstract';
            $article->content = (isset($_POST['content'])) ? $_POST['content'] : 'No Content';
            $article->author = (isset($_POST['author'])) ? $_POST['author'] : 'No Author';
            $article->links = (isset($_POST['links'])) ? $_POST['links'] : '';
            $article->is_fdg = (isset($_POST['is_fdg'])) ? TRUE : FALSE;
            $article->is_fdr = (isset($_POST['is_fdr'])) ? TRUE : FALSE;
            $article->news_image = (isset($_POST['news_image'])) ? $_POST['news_image'] : 'http://placehold.it/100x100';
            $article->article_image = (isset($_POST['article_image'])) ? $_POST['article_image'] : 'http://placehold.it/1170x300';
            list($day, $month, $year) = explode('/', $_POST['publication_date']);
            list($hour, $minutes) = explode(':', $_POST['publication_time']);
            $article->publication_timestamp = new DateTime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minutes . ':00');
            $am->update_article($article);
        } else {
            $article = new Article();
            $article->title = (isset($_POST['title'])) ? $_POST['title'] : 'No Title';
            $article->abstract = (isset($_POST['abstract'])) ? $_POST['abstract'] : 'No Abstract';
            $article->content = (isset($_POST['content'])) ? $_POST['content'] : 'No Content';
            $article->author = (isset($_POST['author'])) ? $_POST['author'] : 'No Author';
            $article->links = (isset($_POST['links'])) ? $_POST['links'] : '';
            $article->is_fdg = (isset($_POST['is_fdg'])) ? TRUE : FALSE;
            $article->is_fdr = (isset($_POST['is_fdr'])) ? TRUE : FALSE;
            $article->news_image = (isset($_POST['news_image'])) ? $_POST['news_image'] : 'http://placehold.it/100x100';
            $article->article_image = (isset($_POST['article_image'])) ? $_POST['article_image'] : 'http://placehold.it/1170x300';
            list($day, $month, $year) = explode('/', $_POST['publication_date']);
            list($hour, $minutes) = explode(':', $_POST['publication_time']);
            $article->publication_timestamp = new DateTime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minutes . ':00');
            $article->seo_title = Article::get_seo_title($article->title);
            $article->creator = $_SESSION['user'];
            $am->save_article($article);
            
            $nm = new NewsManager();
            $news = new News();
            $news->title = $article->title;
            $news->content = $article->abstract;
            $news->link = 'http://www.donboscoland.it' . Site::APP_URL . 'articoli/' . $article->seo_title . '/' . $article->id . '/';
            $news->publication_timestamp = $article->publication_timestamp;
            $news->image = $article->news_image;
            $news->creator = $article->creator;
            $nm->save_news($news);
            
        }
        header('Location: /eventijesolo/');
        exit();
    } else if ($_GET[ACTION] == ACTION_DELETE) {
        if (isset($_GET[ARTICLE_ID])) {
            $am->delete_article($_GET[ARTICLE_ID]);
            header('Location: /eventijesolo/');
            exit();
        }
    }
} else {
    echo '';
}

include ('article.view.php');
?>
